package cn.ruituo.adv2;

import java.util.Objects;

public class Pengiun implements Comparable<Pengiun> {
    private String name;
    private int age;
    private String type;

    //...set
    public Pengiun() {

    }

    public Pengiun(String name, int age, String type) {
        this.name = name;
        this.age = age;
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    //定义企鹅比较的标准

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Pengiun pengiun = (Pengiun) o;
        return Objects.equals(name, pengiun.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }

//    @Override
//    public boolean equals(Object obj) {
//
//        return super.equals(obj);
//    }

    @Override
    public String toString() {
        return "企鹅{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", type='" + type + '\'' +
                '}';
    }

    @Override//把企鹅对象的比较变成其name属性的比较
    public int compareTo(Pengiun o) {
       // System.out.println("比较："+this+"和"+o);
        if(o==null)
            return -1;
        if (this.name == null && o.name == null) return 0;
        if(this.name!=null &&o.name==null)
            return 1;
        if(this.name==null &&o.name!=null)
            return -1;
        return this.name.compareTo(o.name);
    }

//    @Override//把企鹅对象的比较变成其age的比较
//    public int compareTo(Pengiun o) {
//        // System.out.println("比较："+this+"和"+o);
//        if(o==null)
//            return -1;
//        return this.age-o.age;
//    }
}
